package com.loe.ktui.custom

import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.loe.ktui.UiGroup
import com.loe.ktui.UiView

class UiCustomView(id: String? = null) : UiView(id)
{
    var loadView: (() -> View)? = null
    fun loadView(loadView: () -> View)
    {
        this.loadView = loadView
    }

    /** 创建view */
    override fun createView(context: Context, parent: ViewGroup?): View
    {
        if (loadView == null) throw Exception("请实现UiCustomView的loadView方法")
        val view = loadView!!.invoke()
        initUI(context, view)
        addToParent(parent, view)
        return view
    }
}

/**
 * 自定义View，须实现loadView方法
 */
fun Any.ui_CustomView(id: String? = null, block: (UiCustomView.() -> Unit)? = null): UiCustomView
{
    val ui = UiCustomView(id)
    block?.let { ui.it() }
    if (this is UiGroup) addChild(ui)
    return ui
}